package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.ActSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Bean.Userbean;
import com.neusoft.yuejian26.yuejian_26.Other.CacheActInfo;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.ActivityModel;
import com.neusoft.yuejian26.yuejian_26.model.ApplyModel;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;

import retrofit2.Call;

public class ApplyActivity extends AppCompatActivity {

    public ImageView svg_back;
    public TextView act_name,user_name,user_tel;
    public EditText idcard_num,true_name;
    public RelativeLayout btn_apply;
    private String uid=null;
    private String actid=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        intview();
        intEvent();
        getData();
        getActData();
    }

    private void getData(){
        uid=new CacheInfo(ApplyActivity.this).getUserIdCache();
        if(uid!=null) {
            UserModel userModel = new UserModel();
            Call call = userModel.userIntro(uid);
            userModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Userbean userintrodata = (Userbean) o;
                    user_name.setText(userintrodata.getUname());
                    user_tel.setText(userintrodata.getUtel());
                }
            });
        }
    }

    private class subApply implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            String sum = true_name.getText().toString();
            String orderid = idcard_num.getText().toString();
            ApplyModel applyModel = new ApplyModel();
            Call call = applyModel.subApply(sum,orderid);
            applyModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Result result = (Result) o;
                    int state = result.getCode();
                    if(1 == state){
                        Toast.makeText(ApplyActivity.this,"报名成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(ApplyActivity.this,MainActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("page",0);
                        intent.putExtras(bundle);
                        ApplyActivity.this.startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(ApplyActivity.this,"报名失败",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void getActData(){
        actid=new CacheActInfo(ApplyActivity.this).getActIdCache();
        ActivityModel activityModel = new ActivityModel();
        Call call=activityModel.actSimp(actid);
        activityModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                ActSimpbean msimpdata= (ActSimpbean) o;
                act_name.setText(msimpdata.getAname());
            }
        });
    }

    private void intview(){
        act_name= (TextView) findViewById(R.id.act_name);
        user_name= (TextView) findViewById(R.id.user_name);
        user_tel= (TextView) findViewById(R.id.user_tel);
        true_name= (EditText) findViewById(R.id.true_name);
        idcard_num= (EditText) findViewById(R.id.idcard_num);
        btn_apply= (RelativeLayout) findViewById(R.id.btn_apply);
        svg_back=(ImageView) findViewById(R.id.svg_back);
    }

    private void intEvent(){
        btn_apply.setOnClickListener(new subApply());
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
